/**
 * @author Manoj
 * Not in use right now
 */


/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guifilegenerator;

import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;

public class HtmlObjectfinder
{
    //private File f;
    private RandomAccessFile raf;
    private  char  Specialchar;
    private HashMap<Integer,Integer> Eachlinelevel= new HashMap<Integer,Integer>();
    private Hashtable<Object,LinkedList<Object>> object_Properties = new Hashtable<Object,LinkedList<Object>> (); // to save all properties                
    
    public HtmlObjectfinder(String filename,char specialchar)
    {  
    	//this.f= new File(filename);
    	try
        {
           this.raf= new RandomAccessFile(filename,"r");
           FileObjectsfinder objects_propr = new FileObjectsfinder(filename);
           this.object_Properties=objects_propr.getobject_Properties();
        } 
        catch(Exception e) 
    	{
    		e.printStackTrace(); 
    	}
    	this.Specialchar=specialchar;
    }                         

   // Rerurns the Linenumber and its LEVEL.                                                                                      
    public HashMap<Integer,Integer> EachLinelevefinder()
    {       
        String line= null;                  
        HashMap<Integer,Integer> levelLine= new HashMap<Integer,Integer>(60,150);

	    try 
	      {
	            raf.seek(0); 
	            int LineNum=0;
	            raf.readLine();
	
	            while((line=raf.readLine())!=null)
	            {
	            
	            //Iterator it=null;    
	            char[] chararry =line.toCharArray();
	            int Level=0;
	            while(chararry[Level]==Specialchar)
	                {
	                 Level++;
	                }
	                LineNum++;
	
	               //Integer Lev = (Integer) Level;
	               //Integer Linenumber= (Integer)LineNum;
	               //levelLine.put(Linenumber,Lev);    
	                levelLine.put(LineNum,Level);
	           }
	            	           
		  }
		  catch(Exception e)
		  {
			  e.printStackTrace();   
		  }
        
        this.Eachlinelevel=levelLine;                                                   
        return  levelLine;                
    }          
    
    // THIS return The HTML Line OF Attribute FOR Given OBject.
    /**
     * 
     */
    public  synchronized  StringBuffer getAttribute(String objectName)
    { 
	  LinkedList Properties = object_Properties.get(objectName);
	  Iterator It = Properties.iterator();
	  String readline = null;
	  StringBuffer objecttag = new StringBuffer();
	  boolean  isattribute = false;
      
	  while (It.hasNext() && !isattribute )
	  {
         String temp[]=It.next().toString().split(String.valueOf(Specialchar));
         if(temp[0].equalsIgnoreCase("object"))
         {                                         
             String OBJECT=temp[1];
             try
             {   
            	 raf.seek(0);
            	 int Linenum=0;
                 raf.readLine(); 
                 
                 while((readline=raf.readLine())!=null)
                 {
                      char[] chararry =readline.toCharArray();
                      int Level=0;
                      while(chararry[Level]==Specialchar)
                      {
                    	  Level++;
                      }
                      Linenum++;
                      readline = readline.substring(Level);
                        
                      String  tempSplit[]= readline.split(String.valueOf(Specialchar));
                      if(tempSplit[1].equalsIgnoreCase(OBJECT))
                      {
                          break;   
                      }                                                                                           
                 }
               
               Integer currntLinenum=(Integer)Linenum;
               Integer curruntObjlevel=Eachlinelevel.get(currntLinenum);                                                                               
               int nxtlinelevel=1+Integer.parseInt(curruntObjlevel.toString());     
               
               while(true)
               {
                   String SplitString[]=raf.readLine().substring(nxtlinelevel).split(String.valueOf(Specialchar));
                   Linenum++;                                                                                          
               
                   
                   if(SplitString[1].equalsIgnoreCase("attribute"))
                   {      
                	   isattribute=true;
                   }

                   if(Integer.parseInt(Eachlinelevel.get(Linenum).toString())!=nxtlinelevel)
                               {
                                 break;
                               }
                          if(!SplitString[1].equalsIgnoreCase("attribute"))
                                  {
                                        String toappend=SplitString[0]+"=    "+"\""+SplitString[1]+"\"  ";                                                                    
                                        objecttag.append(toappend);
                               } 
                   }
               //System.out.println("Objecttag is "+objecttag);
             }
           
             catch(Exception e )
             {
        	 	e.printStackTrace();
        	 	StringBuffer error =new StringBuffer();
        	 	if(isattribute=true) return  objecttag;
        	 	error.append("Error");
        	 	return  error ; 
             }                                                                                                                                                                                                                
         }
	  }
	  if(isattribute)
	  {
		  return objecttag;
	  }
                      
      StringBuffer error =new StringBuffer();
      error.append("Error");
      return  error ;          
    }     
  
   ///THis returns  the Number of lines from the Given File.
   public int getnumber0fLines()
   {
      int Linenum=-1;
      try
      {
	       raf.seek(0);
	       String Line =null ;
	       while((Line=raf.readLine())!=null)
	       {
	           Linenum++; 
	       }

      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
      return Linenum; 
   }
   
   public StringBuffer getDisplayText(String objectName) 
   {
     	String readline = null;
	    StringBuffer objecttag = new StringBuffer();
	    boolean  ishaveDisolayText = false;
	    
	    try
	    {   
	    	raf.seek(0);
	    	int Linenum=0;
	    	raf.readLine(); 
	    	
	    	while((readline=raf.readLine())!=null)
	    	{
               Linenum++;
               String  tempSplit[]= readline.split(String.valueOf(Specialchar));
               if(readline.lastIndexOf(objectName)!=-1)
               {
                  break;   // serches file for particular Object
               }                                                                                           
	    	}

	    	Integer currntLinenum=(Integer)Linenum;
	    	Integer curruntObjlevel=Eachlinelevel.get(currntLinenum);                                                                               
	    	int nxtlinelevel=1+Integer.parseInt(curruntObjlevel.toString());  
           
	    	while(true)
	    	{
	           String SplitString[]=raf.readLine().substring(nxtlinelevel).split(String.valueOf(Specialchar));
	           Linenum++;                                                                                          
	
	           if(SplitString[0].equalsIgnoreCase("displaytext"))
	           {      
	             ishaveDisolayText=true;
	             objecttag.append(SplitString[1]);
	           }
	
	           if(Integer.parseInt(Eachlinelevel.get(Linenum).toString())!=nxtlinelevel)
               {
                 break;
               }
	           /* if(!SplitString[1].equalsIgnoreCase("attribute"))
                      {
                            String toappend=SplitString[0]+"    "+"\""+SplitString[1]+"\"  ";
                            System.out.println(toappend);
                            objecttag.append(toappend);
                      }  */
	    	}
	    }
	    catch(Exception e )
	    {
	       e.printStackTrace();
	       StringBuffer error =new StringBuffer();
	       error.append("Error");
	       return  error ; 
	    }                               
	    if(ishaveDisolayText)
	    {
	    	return objecttag;
	    }
         StringBuffer error =new StringBuffer();
         error.append("Error");
         return  error ;          
   }   

   public static void main(String [] arg)
   {
	   char ch = '\t';
	   HtmlObjectfinder hof = new HtmlObjectfinder("c://TextFiles//html5.txt",ch);
	   StringBuffer temp  = hof.getAttribute("body5");
	   System.out.println(" The buffer is "+temp);
   }
   
}
